package com.huike.web.controller.system;


import com.huike.clues.service.ISysDictService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.domain.entity.SysDictType;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.poi.ExcelUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {
    @Resource
    private ISysDictService iSysDictService;

    /**
     * 新增字典类型
     * @return
     */
    @PostMapping("/data")
    public AjaxResult data(@RequestBody SysDictData sysDictData){
      iSysDictService.dictDataInsert(sysDictData);
        return AjaxResult.success();
    }

    /**
     * 修改保存字典类型
     * @param sysDictData
     * @return
     */
    @PutMapping("/data")
    public AjaxResult dataUpdate(@RequestBody SysDictData sysDictData){
        iSysDictService.dataUpdate(sysDictData);
        return AjaxResult.success();
    }

    /**
     * 字典数据列表
     * @param sysDictData
     * @return
     */
    @GetMapping("/data/list")
    public TableDataInfo list(SysDictData sysDictData){
        startPage();
      List<SysDictData> dataList=iSysDictService.dataList(sysDictData);
        return getDataTable(dataList);
    }

    @DeleteMapping("/type/clearCache")
    public AjaxResult clearCache(){
        iSysDictService.clearCache();
        return AjaxResult.success();
    }

    /**
     * 查询字典数据详细
     * @param dictCode
     * @return
     */
    @GetMapping("/data/{dictCode}")
    public AjaxResult dataSelectBydictCode(@PathVariable Integer dictCode){
        SysDictData dictData=iSysDictService.dataSelectBydictCode(dictCode);
        return AjaxResult.success(dictData);
    }

    /**
     * 删除字典类型
     * @param
     * @return
     */
    @DeleteMapping("/data/{dictCodes}")
    public AjaxResult dataDelete(@PathVariable List<String> dictCodes) {
        iSysDictService.dataDelete(dictCodes);
        return AjaxResult.success();
    }
    /**
     *字典导出
     * @param
     * @return
     */
    @GetMapping("/data/export")
    public AjaxResult dataExport(SysDictData sysDictData){

        List<SysDictData> dataList = iSysDictService.dataList(sysDictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
        return util.exportExcel(dataList,"字典数据");
    }


    /**
     * 分页查询数据字典类型信息
     * @param dictType
     * @return
     */
   // @PreAuthorize("@ss.hasPermi('system:dictType:list')")
    @GetMapping("/type/list")
    public TableDataInfo list(SysDictType dictType){
        startPage();
        List<SysDictType> dataList= iSysDictService.list(dictType);
        return getDataTable(dataList);
    }

    /**
     * 新增字典类型
     * @param
     * @return
     */
    @PostMapping("/type")
    public AjaxResult type(@RequestBody SysDictType sysDictType){
        iSysDictService.dictTypeInsert(sysDictType);
        return AjaxResult.success();
    }

    /**
     *查询字典类型详细
     * @param dictId
     * @return
     */
    @GetMapping("/type/{dictId}")
    public AjaxResult Type(@PathVariable Integer dictId){
     SysDictType dictType=iSysDictService. typeSelect(dictId);
        return AjaxResult.success(dictType);
    }

    /**
     *字典类型导出
     * @param
     * @return
     */
    @GetMapping("/type/export")
    public AjaxResult typeExport(SysDictType sysDictType){
        List<SysDictType> list = iSysDictService.list(sysDictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<>(SysDictType.class);
        return util.exportExcel(list,"字典类型数据");
    }
    /**
     * 根据字典类型查询字典数据信息
     * @param
     * @return
     */
    @GetMapping("/data/type/{dictType}")
    public AjaxResult dataType(@PathVariable String dictType){
        List<SysDictData> dictData=iSysDictService. dataTypeselect(dictType);
        return AjaxResult.success(dictData);
    }

    /**
     * 修改字典类型
     * @param sysDictType
     * @return
     */
    @PutMapping("/type")
    public AjaxResult dictType(@RequestBody SysDictType sysDictType){
        iSysDictService. dictTypeUpdate(sysDictType);
        return AjaxResult.success();
    }

    /**
     * 获取字典选择框列表
     * @return
     */
    @GetMapping("/type/optionselect`")
    public AjaxResult optionselect(){
     List<SysDictType> sysDictType =iSysDictService. optionselect();
        return AjaxResult.success(sysDictType);
    }

    /**
     * 删除字典类型
     * @param dictIds
     * @return
     */
    @DeleteMapping("/type/{dictIds}")
    public AjaxResult type(@PathVariable List<Integer> dictIds){
       iSysDictService.typeDelete(dictIds);
        return AjaxResult.success();
    }



}
